/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * An abstract class extending JDialog representing a dialog on screen.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since May 2004
 */
public class ElmElasticConnectionsDialog extends SimpleDialog {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	private String gAnlType;
	
	private DecimalField K1dxField;
	private DecimalField K1dyField;
	private DecimalField K1dzField;
	private DecimalField K1rxField;
	private DecimalField K1ryField;
	private DecimalField K1rzField;
	private DecimalField K2dxField;
	private DecimalField K2dyField;
	private DecimalField K2dzField;
	private DecimalField K2rxField;
	private DecimalField K2ryField;
	private DecimalField K2rzField;
	public double K1dx;
	public double K1dy;
	public double K1dz;
	public double K1rx;
	public double K1ry;
	public double K1rz;
	public double K2dx;
	public double K2dy;
	public double K2dz;
	public double K2rx;
	public double K2ry;
	public double K2rz;
	
//*****************************************************************************
	
	public ElmElasticConnectionsDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		gAnlType = parent.getModel().getDiscreteModel().getGlobalAnalysisModel().getType();
		
		setUpFormat();
		setUpFields();
		
		JPanel mainPane = new JPanel();
		mainPane.setPreferredSize(new Dimension(360, 300));
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
		
		JPanel picPanel = new JPanel();
		//picPanel.setMaximumSize(new Dimension(500,100));
		picPanel.add(new JLabel(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/ElasticConnections100.gif")) ));
		mainPane.add(picPanel);
		
		JPanel k1Pane = new JPanel();
		k1Pane.setLayout(new GridLayout(2,3,2,2));
		k1Pane.setMaximumSize(new Dimension(500,90));
		k1Pane.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmElaConDialog01")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
		mainPane.add(k1Pane);
		
		JPanel k1dxPane = new JPanel();
		k1dxPane.add(new JLabel("Kx:"));
		k1dxPane.add(K1dxField);
		k1Pane.add(k1dxPane);
		JPanel k1dyPane = new JPanel();
		k1dyPane.add(new JLabel("Ky:"));
		k1dyPane.add(K1dyField);
		k1Pane.add(k1dyPane);
		JPanel k1dzPane = new JPanel();
		k1dzPane.add(new JLabel("Kz:"));
		k1dzPane.add(K1dzField);
		k1Pane.add(k1dzPane);
		
		JPanel k1rxPane = new JPanel();
		k1rxPane.add(new JLabel("Krx:"));
		k1rxPane.add(K1rxField);
		k1Pane.add(k1rxPane);
		JPanel k1ryPane = new JPanel();
		k1ryPane.add(new JLabel("Kry:"));
		k1ryPane.add(K1ryField);
		k1Pane.add(k1ryPane);
		JPanel k1rzPane = new JPanel();
		k1rzPane.add(new JLabel("Krz:"));
		k1rzPane.add(K1rzField);
		k1Pane.add(k1rzPane);
		
		JPanel k2Pane = new JPanel();
		k2Pane.setLayout(new GridLayout(2,3,2,2));
		k2Pane.setMaximumSize(new Dimension(500,90));
		k2Pane.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmElaConDialog02")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
		mainPane.add(k2Pane);
		
		JPanel k2dxPane = new JPanel();
		k2dxPane.add(new JLabel("Kx:"));
		k2dxPane.add(K2dxField);
		k2Pane.add(k2dxPane);
		JPanel k2dyPane = new JPanel();
		k2dyPane.add(new JLabel("Ky:"));
		k2dyPane.add(K2dyField);
		k2Pane.add(k2dyPane);
		JPanel k2dzPane = new JPanel();
		k2dzPane.add(new JLabel("Kz:"));
		k2dzPane.add(K2dzField);
		k2Pane.add(k2dzPane);
		
		JPanel k2rxPane = new JPanel();
		k2rxPane.add(new JLabel("Krx:"));
		k2rxPane.add(K2rxField);
		k2Pane.add(k2rxPane);
		JPanel k2ryPane = new JPanel();
		k2ryPane.add(new JLabel("Kry:"));
		k2ryPane.add(K2ryField);
		k2Pane.add(k2ryPane);
		JPanel k2rzPane = new JPanel();
		k2rzPane.add(new JLabel("Krz:"));
		k2rzPane.add(K2rzField);
		k2Pane.add(k2rzPane);
		
		if (gAnlType.equals("PlaneFrame")) {
			K1dzField.setEnabled(false);
			K1rxField.setEnabled(false);
			K1ryField.setEnabled(false);
			K2dzField.setEnabled(false);
			K2rxField.setEnabled(false);
			K2ryField.setEnabled(false);
		}
		else if (gAnlType.equals("PlaneTruss")) {
			K1dzField.setEnabled(false);
			K1rxField.setEnabled(false);
			K1ryField.setEnabled(false);
			K1rzField.setEnabled(false);
			K2dzField.setEnabled(false);
			K2rxField.setEnabled(false);
			K2ryField.setEnabled(false);
			K2rzField.setEnabled(false);
		}
		else if (gAnlType.equals("Beam")) {
			K1dxField.setEnabled(false);
			K1dzField.setEnabled(false);
			K1rxField.setEnabled(false);
			K1ryField.setEnabled(false);
			K2dxField.setEnabled(false);
			K2dzField.setEnabled(false);
			K2rxField.setEnabled(false);
			K2ryField.setEnabled(false);
		}
		else if (gAnlType.equals("Grid")) {
			K1dxField.setEnabled(false);
			K1dzField.setEnabled(false);
			K1rxField.setEnabled(false);
			K1ryField.setEnabled(false);
			K2dxField.setEnabled(false);
			K2dzField.setEnabled(false);
			K2rxField.setEnabled(false);
			K2ryField.setEnabled(false);
		}
		
		setPane(mainPane);
	}
	
//*****************************************************************************
	
    public int showDialog() {
    	return super.showDialog(this.parent, parent.getResourceBundle().getString("dElmElaConDialog03"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		try {
			K1dx = K1dxField.getValue();
			K1dy = K1dyField.getValue();
			K1dz = K1dzField.getValue();
			K1rx = K1rxField.getValue();
			K1ry = K1ryField.getValue();
			K1rz = K1rzField.getValue();
			K2dx = K2dxField.getValue();
			K2dy = K2dyField.getValue();
			K2dz = K2dzField.getValue();
			K2rx = K2rxField.getValue();
			K2ry = K2ryField.getValue();
			K2rz = K2rzField.getValue();
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
//*****************************************************************************
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		
		if (this.parent.getController().getSelection().size() == 1 ) {
			
			FrameElement elm = (FrameElement)this.parent.getController().getSelection().get(0);
			
			K1dxField = new DecimalField(0, 7, numberFormat);
			K1dyField = new DecimalField(0, 7, numberFormat);
			K1dzField = new DecimalField(0, 7, numberFormat);
			K1rxField = new DecimalField(0, 7, numberFormat);
			K1ryField = new DecimalField(0, 7, numberFormat);
			K1rzField = new DecimalField(0, 7, numberFormat);
			K2dxField = new DecimalField(0, 7, numberFormat);
			K2dyField = new DecimalField(0, 7, numberFormat);
			K2dzField = new DecimalField(0, 7, numberFormat);
			K2rxField = new DecimalField(0, 7, numberFormat);
			K2ryField = new DecimalField(0, 7, numberFormat);
			K2rzField = new DecimalField(0, 7, numberFormat);
			
			if (elm.getLiberation(0)) K1dxField.setEnabled(false);
			if (elm.getLiberation(1)) K1dyField.setEnabled(false);
			if (elm.getLiberation(2)) K1dzField.setEnabled(false);
			if (elm.getLiberation(3)) K1rxField.setEnabled(false);
			if (elm.getLiberation(4)) K1ryField.setEnabled(false);
			if (elm.getLiberation(5)) K1rzField.setEnabled(false);
			if (elm.getLiberation(6)) K2dxField.setEnabled(false);
			if (elm.getLiberation(7)) K2dyField.setEnabled(false);
			if (elm.getLiberation(8)) K2dzField.setEnabled(false);
			if (elm.getLiberation(9)) K2rxField.setEnabled(false);
			if (elm.getLiberation(10)) K2ryField.setEnabled(false);
			if (elm.getLiberation(11)) K2rzField.setEnabled(false);
			
		}else {
			
			K1dxField = new DecimalField(0, 7, numberFormat);
			K1dyField = new DecimalField(0, 7, numberFormat);
			K1dzField = new DecimalField(0, 7, numberFormat);
			K1rxField = new DecimalField(0, 7, numberFormat);
			K1ryField = new DecimalField(0, 7, numberFormat);
			K1rzField = new DecimalField(0, 7, numberFormat);
			K2dxField = new DecimalField(0, 7, numberFormat);
			K2dyField = new DecimalField(0, 7, numberFormat);
			K2dzField = new DecimalField(0, 7, numberFormat);
			K2rxField = new DecimalField(0, 7, numberFormat);
			K2ryField = new DecimalField(0, 7, numberFormat);
			K2rzField = new DecimalField(0, 7, numberFormat);
		}
	}
	
//*****************************************************************************
}
